package com.calvin.springcloud.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.calvin.springcloud.base.ResponseResult;
import com.calvin.springcloud.model.user.User;
import com.calvin.springcloud.service.user.IUserService;

@RestController
@RequestMapping("user")
public class UserController {

	@Value(value = "${server.port}")
	private String serverPort;

	@Autowired
	private IUserService userService;

	@GetMapping(value = "getUserList")
	public ResponseResult<List<User>> getUserList() {
		return new ResponseResult<List<User>>().success(userService.getUserList(), "提供者端口：" + serverPort);
	}
	
	@GetMapping(value="/findUser/{userId}/{userName}")
	public ResponseResult<User> findUser(@PathVariable(value="userId") Long userId,@PathVariable(value="userName") String userName){
		User user = userService.findUser(userId, userName);
		return new ResponseResult<User>().success(user);
	}

	@GetMapping(value = "/getUserList/timeout")
	public ResponseResult<Object> getUserListTimeOut() {
		// 暂停几秒钟线程
		System.out.println("提供者：" + serverPort);
		try {
			TimeUnit.SECONDS.sleep(3);
			return new ResponseResult<Object>().success("提供者：" + serverPort +"程序暂停三秒") ;
		} catch (Exception e) {
			return new ResponseResult<Object>().fail(e.getMessage());
		}
	}
}
